package edu.zqu.instructionalMatters.project;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @Author 何文东
 * @Date 2020/7/3 14:19
 * @Description: MyBatis Plus 逆向工程
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();//创建generator对象
        DataSourceConfig dataSourceConfig = new DataSourceConfig();//数据源
        dataSourceConfig.setDbType(DbType.MYSQL);//数据库类型
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/education-instructionalmatters?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT");//URL
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");//数据库驱动
        dataSourceConfig.setUsername("root");//用户名
        dataSourceConfig.setPassword("123456");//密码

        autoGenerator.setDataSource(dataSourceConfig);

        /**
         * 全局配置
         */
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/education-instructionalMatters/src/main/java/");//生成文件的存放路径
        globalConfig.setOpen(false);//生成之后，不用自动打开文件目录
        globalConfig.setAuthor("何文东");
        globalConfig.setServiceName("%sService");//生成的Service前不加默认的I
        //globalConfig.setFileOverride(true);//是否覆盖

        autoGenerator.setGlobalConfig(globalConfig);

        /**
         * 包信息
         */
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("edu.zqu.instructionalMatters.project");//逆向工程目标目录，父包里面
        //packageConfig.setModuleName("generator");//在父包下再创建一个包，用于存放所有生成的文件
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("entity");

        autoGenerator.setPackageInfo(packageConfig);

        /**
         * 配置策略
         */
        StrategyConfig strategyConfig = new StrategyConfig();
        //strategyConfig.setInclude("user","book");//想要生成哪些表对应的数据（entity、controller...)，就加入哪个表名
        strategyConfig.setEntityLombokModel(true);//实体类使用lombok注解
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//下划线转驼峰
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setLogicDeleteFieldName("deleted");
        autoGenerator.setStrategy(strategyConfig);

        /**
         * 执行
         */
        autoGenerator.execute();
    }
}
